Java Review
The environment
n
A text editor
is used to record the high level java program in ASCII code
n
A java
compiler translates it into bytecode (J-code)
n
A bytecode
interpreter translates it into the machine language for the specific
machine it is running on
Applications and Applets
n
Applets
n
programs that can be embedded in HTML documents.
n
HTML has tags
specifically designed for Java applets
n
Applications
n
Traditional
programs that can run stand-alone from the command line or a GUI
n
In Java, a main
method is included in a class to make it an application
So What!
n
The platform
independence of Java may be a huge marketing tool, but is actually of little use
to people learning Object Oriented Programming and Abstract Data Types
n
What is of use is
the simplicity of the Java syntax and programming concepts
n
Java is a
"pure" Object Oriented Language
n
encapsulation, inheritance, and polymorphism
n
all code must be contained in a class
n
no free functions (functions that do not belong to some
class) like C++, although someone who wants to write messy Java code certainly
can
n
Is OO the best programming paradigm?
Data TYPES in Java
n
When you program
in Java, every variable belongs to one of two categories
n
Primitive types
n
Reference type
(all objects)
n
The space
allotted to a primitive type holds its value
n
The space
allotted to a reference type holds the address where the object is
stored in dynamic memory
n
At compile time,
no objects have been created so the address is null
n
Objects are
created as the program runs
Basic or Primitive types
Four integers type
byte -- 8 bit signed integral value (-128 to 127)
short -- 16-bit signed
(-32,768 to 32,767)
int-- 32 bit
signed (most commonly used)
long -- 64 bits
Floats
float – 32 bits (6 to 7 sig decimal digits , exponent to
38)
double – 64 bits (15 sig decimal digits, exponent to 308)
boolean
n
Can only be true
or false
char
n
Stored in 16 bit
ASCII code
n
Java does not use
8-bit ASCII code
User written classes
n
The class often
has more than one constructor
n
They should have
accessors and mutators
n
If an object of
the class will ever be printed it should have a toString method
n
The toString
method is inherited from the Object class
n
Most classes
should override the inherited method
The Object class
n
This is the
common base or ancestor class from which all Java classes are derived
n
All variables of
reference type are instances of Object, including
arrays
n
Every method of Object is inherited, directly or indirectly by all other
classes
n
This means that
the Object class provides a set of methods that all objects have
n
To see all the
methods contained in the Object class, (or any other) check the Java
API
Constructors
EX: Person Jean = new Person(
)
n
This is a call to
the Person constructor (a method)
n
Person is the class
name, Jean is the object name
n
Constructor name
is the same as the class name
n
The constructor
method creates a new instance of a specified class
n
In Java,
constructors are usually invoked (or called) through the new keyword
n
Since a
constructor is a method (i.e. function) it must be written just like other
methods
Constructors
can be overloaded
Person Bill =
new Person(“Bill”, “Smith”, etc. )
= = and the equals method
n
The = = operation
just compares what is stored in the named storage locations
n
For primitives,
that is actual values
n
For object, it is
addresses
n
The equals method
is also inherited from the Object class
n
It should be
overridden so that it compares the data found at an address, not the addresses
Java API
n
http://java.sun.com/j2se/1.5.0/docs/api/